//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.mdiy.action.web;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.mdiy.action.BaseAction;
import net.mingsoft.mdiy.biz.IModelBiz;
import net.mingsoft.mdiy.biz.IModelDataBiz;
import net.mingsoft.mdiy.constant.e.ModelCustomTypeEnum;
import net.mingsoft.mdiy.entity.ModelEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Api(
        tags = {"前端-自定义模块接口"}
)
@Controller("webFormDataAction")
@RequestMapping({"/mdiy/form/data"})
public class FormDataAction extends BaseAction {
    @Autowired
    private IModelDataBiz modelDataBiz;
    @Autowired
    private IModelBiz modelBiz;

    public FormDataAction() {
    }

    @ApiOperation("保存")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "modelName",
            value = "业务模型名称",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "modelId",
            value = "业务模型Id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "rand_code",
            value = "验证码",
            required = false,
            paramType = "query"
    )})
    @PostMapping({"save"})
    @ResponseBody
    public ResultData save(HttpServletRequest request, HttpServletResponse response) {
        String modelName = BasicUtil.getString("modelName");
        String modelId = BasicUtil.getString("modelId");
        if (StringUtils.isEmpty(modelName) && StringUtils.isEmpty(modelId)) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("model.name")}));
        } else {
            this.LOG.debug("保存表单");
            LambdaQueryWrapper<ModelEntity> wrapper = new LambdaQueryWrapper();
            wrapper.eq(StringUtils.isNotEmpty(modelName), ModelEntity::getModelName, modelName)
                    .eq(StringUtils.isNotEmpty(modelId), ModelEntity::getId, modelId)
                    .eq(ModelEntity::getModelCustomType, ModelCustomTypeEnum.FORM.getLabel());
            ModelEntity model = (ModelEntity)this.modelBiz.getOne(wrapper, true);
            if (model == null) {
                return ResultData.build().error(this.getResString("err.not.exist", new String[]{this.getResString("model.name")}));
            } else {
                if (!Boolean.parseBoolean(((Map)JSONUtil.toBean(model.getModelJson(), Map.class)).get("isWebSubmit").toString())) {
                    ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("model.name")}));
                }

                if ((ObjectUtil.isNull(((Map)JSONUtil.toBean(model.getModelJson(), Map.class)).get("isWebCode")) || Boolean.parseBoolean(((Map)JSONUtil.toBean(model.getModelJson(), Map.class)).get("isWebCode").toString())) && !this.checkRandCode()) {
                    this.LOG.debug("验证码错误");
                    return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("rand.code")}));
                } else {
                    Map<String, Object> map = BasicUtil.assemblyRequestMap();
                    return this.modelDataBiz.saveDiyFormData(model.getId(), map) ? ResultData.build().success() : ResultData.build().error(this.getResString("fail", new String[]{this.getResString("save")}));
                }
            }
        }
    }

    @ApiOperation("提供前端查询自定义表单提交数据")
    @ApiImplicitParam(
            name = "modelName",
            value = "业务名称",
            required = true,
            paramType = "query"
    )
    @GetMapping({"list"})
    @ResponseBody
    public ResultData list(@RequestParam("modelName") String modelName, HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper<ModelEntity> queryWrapper = new QueryWrapper();
        queryWrapper.like("model_name", modelName);
        ModelEntity model = (ModelEntity)this.modelBiz.getOne(queryWrapper);
        if (model != null) {
            if (!Boolean.parseBoolean(((Map)JSONUtil.toBean(model.getModelJson(), Map.class)).get("isWebSubmit").toString())) {
                ResultData.build().error();
            }

            List list = this.modelDataBiz.queryDiyFormData(model.getId(), BasicUtil.assemblyRequestMap());
            return ObjectUtil.isNotNull(list) ? ResultData.build().success(new EUListBean(list, (int)BasicUtil.endPage(list).getTotal())) : ResultData.build().error();
        } else {
            return ResultData.build().error();
        }
    }
}
